package org.song.demo.listvideo;

import android.graphics.Rect;
import android.view.View;

/**
 * Created by song on 2017/6/1.
 */

public class ListCalculator {
    private Getter getter;
    private CallBack callBack;

    private boolean isScrollUp;
    private int INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS = 70;


    public ListCalculator(Getter getter, CallBack callBack) {
        this.getter = getter;
        this.callBack = callBack;
    }

    public int getCurrentItem() {
        return currentItem;
    }

    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }

    private int currentItem = 0;//当前活动的item

    public void setScrollIdel(boolean scrollIdel) {
        isScrollIdel = scrollIdel;
    }

    private boolean isScrollIdel = true;//是否停止滚动才设置播放

    /**
     * 滚动中
     */
    public void onScrolling() {
        if (!checkUnDown())
            return;
        int firstVisiblePosition = getter.getFirstVisiblePosition();
        int lastVisiblePosition = getter.getLastVisiblePosition();

        View currentView = getter.getChildAt(currentItem - firstVisiblePosition);
        int currentP = getVisibilityPercents(currentView);
        if (isScrollUp) {
            if (lastVisiblePosition >= currentItem + 1) {//存在下一个item
                View nextView = getter.getChildAt(currentItem - firstVisiblePosition + 1);
                int nextP = getVisibilityPercents(nextView);
                if (enoughPercentsForDeactivation(currentP, nextP)) {
                    callBack.deactivate(currentView, currentItem);
                    if (!isScrollIdel)
                        callBack.setActive(nextView, currentItem + 1);
                    currentItem++;
                }

            }

        } else {
            if (firstVisiblePosition <= currentItem - 1) {//存在上一个item
                View preView = getter.getChildAt(currentItem - firstVisiblePosition - 1);
                int preP = getVisibilityPercents(preView);
                if (enoughPercentsForDeactivation(currentP, preP)) {
                    callBack.deactivate(currentView, currentItem);
                    if (!isScrollIdel)
                        callBack.setActive(preView, currentItem - 1);
                    currentItem--;
                }
            }
        }


    }

    private boolean enoughPercentsForDeactivation(int visibilityPercents, int nextVisibilityPercents) {
        return (visibilityPercents < INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS &&
                nextVisibilityPercents >= INACTIVE_LIST_ITEM_VISIBILITY_PERCENTS) || visibilityPercents <= 1;
    }

    /**
     * 停止滚动
     */
    public void onScrolled() {
        int firstVisiblePosition = getter.getFirstVisiblePosition();
        int lastVisiblePosition = getter.getLastVisiblePosition();
        int temp_currentItem = 0;

        if (lastVisiblePosition > firstVisiblePosition) {
            View view;
            View nextView;
            if (isScrollUp) {
                view = getter.getChildAt(0);
                nextView = getter.getChildAt(1);
                int p = getVisibilityPercents(view);
                int nextp = getVisibilityPercents(nextView);
                if (enoughPercentsForDeactivation(p, nextp))
                    temp_currentItem = firstVisiblePosition + 1;
                else
                    temp_currentItem = firstVisiblePosition;
            } else {
                view = getter.getChildAt(lastVisiblePosition - firstVisiblePosition);
                nextView = getter.getChildAt(lastVisiblePosition - firstVisiblePosition - 1);
                int p = getVisibilityPercents(view);
                int nextp = getVisibilityPercents(nextView);
                if (enoughPercentsForDeactivation(p, nextp))
                    temp_currentItem = lastVisiblePosition - 1;
                else
                    temp_currentItem = lastVisiblePosition;
            }


        } else {
            temp_currentItem = firstVisiblePosition;
        }
        if (temp_currentItem != currentItem) {
            currentItem = temp_currentItem;
            callBack.setActive(getter.getChildAt(currentItem - firstVisiblePosition), currentItem);
        }

    }


    private int mOldTop;
    private int mOldFirstVisibleItem;

    //检测滑动方向
    private boolean checkUnDown() {
        View view = getter.getChildAt(0);
        if (view == null)
            return false;
        int top = view.getTop();

        int firstVisibleItem = getter.getFirstVisiblePosition();
        if (firstVisibleItem == mOldFirstVisibleItem) {
            if (top > mOldTop) {
                isScrollUp = false;
            } else if (top < mOldTop) {
                isScrollUp = true;
            }
        } else {
            isScrollUp = firstVisibleItem > mOldFirstVisibleItem;
        }

        mOldTop = top;
        mOldFirstVisibleItem = firstVisibleItem;
        return true;
    }


    public static int getVisibilityPercents(View view) {
        final Rect currentViewRect = new Rect();

        int percents = 100;

        int height = (view == null || view.getVisibility() != View.VISIBLE) ? 0 : view.getHeight();

        if (height == 0) {
            return 0;
        }

        view.getLocalVisibleRect(currentViewRect);

        if (viewIsPartiallyHiddenTop(currentViewRect)) {
            // view is partially hidden behind the top edge
            percents = (height - currentViewRect.top) * 100 / height;
        } else if (viewIsPartiallyHiddenBottom(currentViewRect, height)) {
            percents = currentViewRect.bottom * 100 / height;
        }

        return percents;
    }

    private static boolean viewIsPartiallyHiddenBottom(Rect currentViewRect, int height) {
        return currentViewRect.bottom > 0 && currentViewRect.bottom < height;
    }

    private static boolean viewIsPartiallyHiddenTop(Rect currentViewRect) {
        return currentViewRect.top > 0;
    }
}
